<?php
namespace app\admin\controller;

use app\admin\model\AgentLevel as AgentLevelModel;
use think\Db;
use think\Request;

class AgentLevel extends Base
{
    public function index()
    {
        $model = new AgentLevelModel;


        //渲染页面模板
        $m = $model->field('a.id,a.name,a.illustration,a.created_at,d.username,g.countid')
            ->alias('a')
            ->join('(SELECT franchiser_group_id, COUNT(id) as countid from '. $this->dbTablePrefix .'franchiser GROUP BY franchiser_group_id HAVING franchiser_group_id>0) g','a.id = g.franchiser_group_id ','left')
            ->join('admin d','a.create_admin_id = d.id','left')
            ->order('a.id','desc');
        //->select();
        // ->paginate(5);

        $result = $m->paginate(10);

        //计算总共记录条数
        $count = $result->total();
        //分页
        $page = $result->render();

        //接收ajax传递的值
        $request = request();
        $ispost = $request->isPost();
        if($ispost){
            $id = $_POST["viewId"];
            $view = $model->getAll()->where("id",$id)->find();
            return json([
                'code' => 200,
                'view'=>$view,
            ]);
        }


        $this->assign('currentPage',$result->currentPage());
        $this->assign('lastPage',$result->lastPage());
        $this->assign('page',$page);
        $this->assign('agentLevel',$result);
        $this->assign('count',$count);
        $this->assign('empty',"暂无数据");
        return $this->fetch();
    }

    //新增数据
    public function create(){
        $model = new AgentLevelModel;
        //接收ajax传递的值
        $ispost = $this->request->isPost();
        if($ispost){
            $formdata = $this->request->post();
            $username = Db::query("select id,name from ". $this->dbTablePrefix ."franchiser_level where `name`='".$formdata['name']."' and deleted_at is NULL ");
            
            if(!empty($username)){
                return json([
                    'code' => 4000,
                    'message'=>"用户名已存在"
                ]);
            }
            $result = $model->allowField(true)->save($formdata);

            if($result == 1){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }

    //修改数据处理
    public function edit($id)
    {
        $model = new AgentLevelModel;
        //接收ajax传递的值
        $ispost = $this->request->isPost();

        if($ispost){
            $formdata = $this->request->post();

            $result = $model->allowField(true)->save($formdata,['id'=>$id]);

            if($result == 1){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }

    //删除单条记录
    public function delete($id)
    {
        $result = AgentLevelModel::update(['deleted_at'=>date('Y-m-d H:i:s')],['id'=>$id]);//软删除
        if($result){
            return json([
                'code' => 200,
            ]);
        }

    }

    //批量删除
    public function deleteArr()
    {
        $ispost = $this->request->isPost();
        if ($ispost) {
            $idArr = $this->request->post();

            $result = AgentLevelModel::where('id','exp','in('.implode(',',$idArr['id']).')')->update(['deleted_at'=>date('Y-m-d H:i:s')]);//软删除

            if($result){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }
}